<template>
  <div class="app-container">
    <UploadExcel :on-success="handleSuccess" :before-upload="beforeUpload" />
    <!-- <el-table
      :data="tableData"
      border
      highlight-current-row
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column
        v-for="(item, index) of tableHeader"
        :key="index"
        :prop="item"
        :label="item"
      />
    </el-table> -->
  </div>
</template>

<script>
import dayjs from 'dayjs';
import {importEmployee} from "@/api/employees"
export default {
  name: "upload",
  data() {
    return {
      tableData: [],
      tableHeader: [],
    };
  },
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (isLt1M) {
        return true;
      }
      this.$message({
        message: "请不要上传大于1MB的文件",
        type: "warning",
      });
      return false;
    },
    async handleSuccess({ results, header }) {
      // this.tableData = results;
      this.tableHeader = header;
      const userRelations = {
        入职日期: "timeOfEntry",
        手机号: "mobile",
        用户名: "username",
        部门: "departmentName",
        聘用形式: "formOfEmployment",
        转正日期: "correctionTime",
      };
      const arr = [];
      console.log("header", this.tableHeader);
      results.forEach((item) => {
        const obj = {}
        Object.keys(item).forEach((key) => {
            obj[userRelations[key]] = item[key];
        });
        arr.push(obj);
      });
      arr.forEach(item=>{
        item.correctionTime  = dayjs(item.correctionTime).format("YYYY-MM-DD");
        item.timeOfEntry = dayjs(item.timeOfEntry).format("YYYY-MM-DD");
      })
      this.tableData =arr;
      console.log("tabledata",this.tableData);
    try {
       let result =  await importEmployee(this.tableData);
     console.log(result);
     this.$message({
      type:"success",
      message:"导入员工成功"
     })
    this.$router.back();
    } catch (error) {
        this.$message({
      type:"info",
      message:"导入员工失败"
     })
    }
    },
  },
};
</script>